<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="function.jsp"%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑新闻</title>
    <link  href="../images/favicon.ico" rel="shortcut icon" type="image/x-icon">
    <link rel="stylesheet" href="../css/messages_doctor_mes_add.css?cache=<%=Math.random()%>">
    <link rel="stylesheet" href="../css/sweetalert.css?cache=<%=Math.random()%>" >
    <link href="https://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="../css/public.css?cache=<%=Math.random()%>">
    <script src="../js/jquery-1.9.1.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <link href="../css/editor/summernote.css" rel="stylesheet">
    <script src="../css/editor/summernote.js"></script>
</head>
<body>
<%@ include file="../header.jsp"%>
<!--内容-->
<div class="containerone">
    <%@ include file="../left_menu.jsp"%>
    <!--右边内容-->
    <div class="right_cont">

        <!--右边内容-->
        <!--右边内容-->

        <div class="message_hos_news_container" id="edit_content">
            <div class="add_doctor_tittle" v-html="option"></div>
            <div class="add_doctor">
                <span>编辑新闻</span>
                <a href="javascript:void(0);"><button class="back_doctor_mes" @click="back()" >返回</button></a>
            </div>
            <%--编辑新闻开始--%>


            <%--编辑新闻开始--%>
            <div class="news_eidt_title"><span>新闻标题:</span><input class="news_add_tittle" name="title" v-model="news_detail.title" type="text" required >
                <%--<p class="news_title_tip" >新闻标题不要超过50字</p>--%>
                <div class="clearfix"></div>
            </div>
            <div class="news_eidt_title"><span>作者:</span><input class="news_add_author" name="author" v-model="news_detail.author" type="text">
                <%--<p class="news_author_tip">作者不要超过50字</p>--%>
                <div class="clearfix"></div>
            </div>
            <div class='doc_good_at'>
                <div class='doc_good_at_tip'><span>新闻简介:</span>(限输入200字符)</div>
                <div class='doc_good_at_textarea'>
                    <textarea name="summary" id="" style="width:900px;height:150px;"  class="news_add_summary"  v-model="news_detail.summary"></textarea>
                </div>
            </div>
            <%--<p class="news_summary_tip">请输入新闻简介</p>--%>
            <div class='doc_good_at'>
                <div class='doc_good_at_tip'><span>新闻内容:</span></div>
                <div class='doc_good_at_textarea'>
                    <div style="width:900px;">
                        <div id="editor" ></div>
                    </div>
                </div>
                <%--<p class="news_content_tip">请输入新闻内容</p>--%>
                <div class="add_doctor_cancel_sure">
                    <button @click="cancel()" class="cancel_news_sure add_cancel">取消</button>
                    <button @click="submit()" class="add_news_sure add_sure" >确定</button>
                </div>
            </div>
            <%--编辑新闻结束--%>
            <%--医生编辑取消和确定开始--%>

            <div class="clearfix"></div>
            <%--医生编辑取消和确定结束--%>
        </div>
    </div>
</div>
</div>

<script src="../js/sweetalert-dev.js"></script>

<script type="text/javascript">
    new Vue({
        el:'#edit_content',
        data:{
            labelId:'',
            news_id:'',
            option:'',
            news_detail:[]
        },
        ready:function(){
            $(document).ready(function() {
                $('#editor').summernote({
                    height:260,
                    lang:'zh-CN',
                    callbacks: {
                        onImageUpload: function(files) {
                            // upload image to server and create imgNode...
                            var img = localImg(files[0]);
                        }
                    }
                });
            });
            function localImg(file) {
                var xmlhttprequest;
                var form = new FormData();
                form.append("file", file);
                if (window.XMLHttpRequest) {
                    xmlhttprequest = new XMLHttpRequest();
                    if (xmlhttprequest.overrideMimeType) {
                        xmlhttprequest.overrideMimeType("text/xml");
                    }
                }
                xmlhttprequest.open("POST", "http://cloud.eeesys.com/pu/upload.php", true);
                xmlhttprequest.onreadystatechange = callback.bind(this);
                xmlhttprequest.send(form);
                function callback() {
                    if (xmlhttprequest.readyState == 4 && xmlhttprequest.status == 200) {
                        var res = JSON.parse(xmlhttprequest.responseText);
                        if (res.errcode == 0) {
                            $('#editor').summernote('insertImage', res.img);
                        } else {
                            swal({
                                txt: decodeURI(res.errmsg),
                                title: "",
                            });
                        }
                    }
                }
            }
            <%--$('.news_title_tip,.news_author_tip,.news_summary_tip,.news_content_tip').hide()--%>
            var self = this;
            var edit_content = getHrefParms("edit_content");
            self.labelId = edit_content.labelId;
            self.news_id = edit_content.news_id;
            self.option = edit_content.option;
            console.log(self);
            $.ajax({
                type: 'POST',
                url: "edit_content.jsp",
                data: {
                    act: "news",
                    news_id:self.news_id,
                },
                success: function(res) {
                    res = JSON.parse(res);
                    console.log(res);
                    if (res.errcode == 0) {
                        self.news_detail = res.data;
                        var sHTML = self.news_detail.html;
                        $('.note-editable').html(sHTML) ;
                    } else {
                        swal("相关信息");
                    }
                },
            });
        },
        methods:{
            decode: function(i) {
                return decodeURI(i);
            },
            cancel:function(){
                location.href="content.jsp";
            },
            back:function(){
                location.href="content.jsp";
            },
            submit:function(){
                var self = this;
                var news_title=self.news_detail.title;
                var news_author=self.news_detail.author
                if(news_title.trim()==""){
                    swal('用户名不能为空')
                    <%--$('.news_title_tip').text('用户名不能为空').show().delay(1000).hide(300);--%>
                    return;
                }else if(news_title.length>50){
                    swal('新闻标题不能超过50个字')
                }
                else if(news_author.trim()==""){
                    $('.news_author_tip').show().delay(1000).hide(300);
                    return;
                }else if(news_author.length>50){
                    swal('作者不能超过50个字')
                }
                else{
                    var sHTML = $('.note-editable').html();

                    $.ajax({
                        type: 'POST',
                        url: "edit_content.jsp",
                        data: {
                            act: "update_news",
                            news_id:self.news_detail.news_id,
                            title:self.news_detail.title,
                            author:self.news_detail.author,
                            summary:self.news_detail.summary,
                            content:encodeURI(sHTML.replace(/"/g, "'")),
                        },
                        success: function(res) {

                            res = JSON.parse(res);
                            if (res.errcode == 0) {
                                location.href="content.jsp";
                            } else {
                                swal(res.errmsg);
                            }
                        },
                    });
                }
            },
        }
    })

</script>
</body>
</html>
